<?php

namespace asyncService\lib\pool;


use asyncService\Factory;

class redisPool extends Factory
{
    static function getConfigKey(){
        return "redis";
    }

    static function getInstance(){
        $config = self::getConfig();
        return self::Produce($config);
    }

    /**
     * @param bool $config
     * @return \Redis
     */
    static function Produce($config = false){
        $pid = getmypid();
        if(isset($redisles[$pid])){
            $redis = $redisles[$pid];
        }else{
            $redis = new \Redis();
            $redis->connect($config['host'],$config['port'],$config['timeout']);
            $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
            $redisles[$pid] = $redis;
            return $redis;
        }

        try{
            $redis->ping();
        }catch (\Exception $exception){
            sleep(1);
            $redis = new \Redis();
            $redis->connect($config['host'],$config['port'],$config['timeout']);
            $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
            $redisles[$pid] = $redis;
        }

        return $redis;

    }
}